{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ddae6140",
   "metadata": {},
   "source": [
    "* 本周主要内容：图像识别（人脸识别）\n",
    "* 日期：2023-03-20-week05\n",
    "* 记录人：刘石珍 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cfd993e",
   "metadata": {},
   "source": [
    "# 上周回顾  \n",
    "## 1.人脸识别概念和基础  \n",
    "## 2.人脸识别应用  \n",
    "## 3.人脸识别实践-face++  \n",
    "> 1.API文档阅读规范  \n",
    "> 2.人脸检测（人脸分析）实践  \n",
    "> 3.输入/输出  \n",
    "> 4.错误码寻找原因  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a23ffde",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.人脸对比  \n",
    "> 2.人脸搜索  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "954f8b75",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d9eb18eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679881151,7f633f2b-506b-4c97-bfb0-b31d57ec2a89', 'time_used': 945, 'confidence': 34.408, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': '48c812124888c6f1f3817dd51cc951c3', 'face_rectangle': {'top': 462, 'left': 336, 'width': 437, 'height': 437}}], 'faces2': [{'face_token': '7b6ca7ae0132dedd0ca78010ef7ba543', 'face_rectangle': {'top': 1020, 'left': 410, 'width': 376, 'height': 376}}], 'image_id1': 'CpQmCQ+yET7k1IbMTfDY9g==', 'image_id2': 'BYEu+0Qqe4zrr6TssP1gKA=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('chi.png', 'rb')\n",
    "f_02 = open('li.jpeg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"0-ePdEz8tHKevHo9diua4pfotiecQc5X\",\n",
    "    \"api_secret\":\"TgtmpFrb2Hmwr6gDpbBV8XBPC9ezB5YD\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print(response.json())\n",
    "print(response.status_code)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25ca1d50",
   "metadata": {},
   "source": [
    "# 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93cfc4af",
   "metadata": {},
   "source": [
    "## 2.1人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e4d86ac",
   "metadata": {},
   "source": [
    "### 2.1.1创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1a0a4175",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': 'b2d5a9da59b56aaa98b398e80d38e7af', 'time_used': 185, 'face_count': 0, 'face_added': 0, 'request_id': '1679881856,98f7d139-10f3-4261-b13d-2ea83ae2bc1e', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"0-ePdEz8tHKevHo9diua4pfotiecQc5X\",\n",
    "    \"api_secret\":\"TgtmpFrb2Hmwr6gDpbBV8XBPC9ezB5YD\",\n",
    "    \"display_name\":\"广州南方学院1栋宿舍楼\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print(response.status_code)\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e881881f",
   "metadata": {},
   "source": [
    "### 2.1.2检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "75abd108",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'b2d5a9da59b56aaa98b398e80d38e7af',\n",
       " 'tags': '',\n",
       " 'time_used': 89,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院1栋宿舍楼',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1679881986,3c8b5717-096b-423a-9072-a30a1cdb12f9',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"0-ePdEz8tHKevHo9diua4pfotiecQc5X\",\n",
    "    \"api_secret\":\"TgtmpFrb2Hmwr6gDpbBV8XBPC9ezB5YD\",\n",
    "    \"faceset_token\":\"b2d5a9da59b56aaa98b398e80d38e7af\"\n",
    "\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params,)\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b2feda6",
   "metadata": {},
   "source": [
    "### 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "25857f8f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2381434380.py, line 15)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn [4], line 15\u001b[1;36m\u001b[0m\n\u001b[1;33m    \"face_tokens\":\u001b[0m\n\u001b[1;37m                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "f = open('1栋/gqq.jpg', 'rb')\n",
    "params = {\n",
    "    \"api_key\":\"0-ePdEz8tHKevHo9diua4pfotiecQc5X\",\n",
    "    \"api_secret\":\"TgtmpFrb2Hmwr6gDpbBV8XBPC9ezB5YD\",\n",
    "    \"faceset_token\":\"\"\n",
    "    \"face_tokens\":\n",
    "\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params,)\n",
    "print(response.status_code)\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9159ff72",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
